<!DOCTYPE html>
<title>Test for selection with collapsed whitespace</title>
<body>
This test checks that characters can be selected correctly with collapsed whitespace.
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="300" height="300">
  <text id="text1" x="20" y="20">     happy           debugging       !!</text>
  <text id="text2" x="20" y="40">     <tspan id="tspan1" style="font-weight: bold;">happy</tspan>           debugging       !!</text>
  <text id="text3" x="20" y="80">     <tspan id="tspan2" style="font-size: 25px;">happy</tspan>          debugging       !!</text>
  <text id="text4" x="20" y="230" fill="black" transform="scale(0.5)" font-size="40">     happy           debugging       !!</text>
</svg>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script src="resources/SelectionTestCase.js"></script>
<script>
test(function() {
  selectText('text1', 0, 3);
  verify(5, 9);

  selectText('text1', 0, 8);
  verify(5, 24);

  selectText('text1', 4, 9);
  verify(9, 25);

  selectText('text2', 0, 3);
  verify(0, 4);

  selectText('text2', 0, 8);
  verify(0, 14);

  selectText('text2', 3, 9);
  verify(3, 15);

  selectText('text3', 0, 3);
  verify(0, 4);

  selectText('text3', 0, 10);
  verify(0, 15);

  selectText('text3', 2, 14);
  verify(2, 19);

  selectText('text4', 0, 3);
  verify(5, 9);

  selectText('text4', 0, 8);
  verify(5, 24);

  selectText('text4', 6, 11);
  verify(21, 27)

  function verify(start, end) {
    var range = window.getSelection().getRangeAt(0);
    assert_equals(range.startOffset, start);
    assert_equals(range.endOffset, end);
    if (window.eventSender) {
      eventSender.mouseMoveTo(0,0);
      eventSender.mouseDown();
      eventSender.mouseUp();
    }
  }

  function selectText(id, start, end) {
    var element = document.getElementById(id);
    var startPos = element.getStartPositionOfChar(start);
    var endPos = element.getEndPositionOfChar(end);
    var absStartPos = toAbsoluteCoordinates(startPos, element);
    var absEndPos = toAbsoluteCoordinates(endPos, element);
    if (window.eventSender) {
      eventSender.mouseMoveTo(absStartPos.x, absStartPos.y);
      eventSender.mouseDown();
      eventSender.mouseMoveTo(absEndPos.x, absEndPos.y);
      eventSender.mouseUp();
    }
  }
});
</script>
